//
//  XLVerificationCodeButton.m
//  campus
//
//  Created by xl on 2019/11/11.
//  Copyright © 2019 xl. All rights reserved.
//

#import "XLVerificationCodeButton.h"

@interface XLVerificationCodeButton()

@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation XLVerificationCodeButton

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initValue];
    }
    return self;
}

- (void)initValue
{
    [[self titleLabel] setFont:[UIFont systemFontOfSize:12]];
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self setTitleColor:[UIColor blueFontColor] forState:UIControlStateNormal];
    self.layer.borderColor = [UIColor blueFontColor].CGColor;
    self.layer.borderWidth = 0.5;
    self.layer.cornerRadius = 18;
    [self addTarget:self action:@selector(verificationCodeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.userInteractionEnabled = YES;
}

#pragma mark - publicMethod
- (void)lockVerificationCodeEnable:(BOOL)enable
{
    self.userInteractionEnabled = enable;
    [self setButtonAlpha:enable?1:0.3];
}

- (void)starCountTime
{
    self.userInteractionEnabled = NO;
    __block int timeout= 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    WEAKSELF
    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(self.timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            STRONGSELF
            [strongSelf stopCountTime];
            
        }else{
            
            NSString *strTime = [NSString stringWithFormat:@"已发送(%ds)", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self setTitle:strTime forState:UIControlStateNormal];
                [self setTitleColor:[UIColor grayFontColor] forState:UIControlStateNormal];
                self.layer.borderColor = [UIColor grayFontColor].CGColor;
                [UIView commitAnimations];
                
                self.userInteractionEnabled = NO;
            });
            
            timeout--;
        }
    });
    dispatch_resume(self.timer);
}

- (void)stopCountTime
{
    if (_timer) {
        dispatch_source_cancel(self.timer);
        dispatch_async(dispatch_get_main_queue(), ^{
            //设置界面的按钮显示 根据自己需求设置
            [self setTitle:@"重新获取" forState:UIControlStateNormal];
            [self setTitleColor:[UIColor blueFontColor] forState:UIControlStateNormal];
            self.layer.borderColor = [UIColor blueFontColor].CGColor;
            self.userInteractionEnabled = YES;
            [self setButtonAlpha:1];
        });
    }
}
#pragma mark - buttonEvent
- (void)verificationCodeButtonClicked:(id)sender
{
    if ([_buttonDelegate respondsToSelector:@selector(willGetVerificationCode)]) {
        [_buttonDelegate willGetVerificationCode];
    }
}


@end
